/**
 * 
 */
package com.iwords.utils;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * @author       BOLIN.HBC 
 * @time         2011-3-12    
 * @description  Date tools       
 */
 

public class DateUtils {

	static SimpleDateFormat simpleA = new SimpleDateFormat("yyyy-MM-dd");
	
	static SimpleDateFormat simple = new SimpleDateFormat("yyyyMMdd");

	public static Date getDate_yyyyMMdd(String date) {
		try {
			return simple.parse(date);
		} catch (ParseException ex) {
			Logger.getLogger(DateUtils.class.getName()).log(Level.SEVERE, null, ex);
		}
		return null;
	}
	
	public static String getDateStr(Date date){

		try {
			return simpleA.format(date);
		} catch (Exception ex) {
			Logger.getLogger(DateUtils.class.getName()).log(Level.SEVERE, null, ex);
		}
		return null;
	
	}

	public static String getDate_yyyyMMdd(Date date) {
		try {
			return simple.format(date);
		} catch (Exception ex) {
			Logger.getLogger(DateUtils.class.getName()).log(Level.SEVERE, null, ex);
		}
		return null;
	}
	
	public static Date getFormatDateTime(String currDate, String format) {
		SimpleDateFormat dtFormatdB = null;
		try {
			dtFormatdB = new SimpleDateFormat(format);
			return dtFormatdB.parse(currDate);
		} catch (Exception e) {
			dtFormatdB = new SimpleDateFormat("yyyyMMdd");
			try {
				return dtFormatdB.parse(currDate);
			} catch (Exception ex){}
		}
		return null;
	}
	
	public static String getFormatDateTime(java.util.Date currDate, String format) {
		SimpleDateFormat dtFormatdB = null;
		try {
			dtFormatdB = new SimpleDateFormat(format);
			return dtFormatdB.format(currDate);
		} catch (Exception e) {
			dtFormatdB = new SimpleDateFormat("yyyyMMdd");
			return dtFormatdB.format(currDate);
		}
	}
	
	public static Date getDateBeforeOrAfter(Date curDate, int iDate) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(curDate);
		cal.add(Calendar.DAY_OF_MONTH, iDate);
		return cal.getTime();
	}
}

